/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.digitalize.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.dao.DigitalizeMessageDao;
import com.jeesite.modules.digitalize.entity.DigitalizeMessage;
import com.jeesite.modules.digitalize.entity.dto.componentMessage.MessageQuery;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * component_messageService
 *
 * @author patronli
 * @version 2023-09-08
 */
@Service
public class DigitalizeMessageService extends ServiceImpl<DigitalizeMessageDao, DigitalizeMessage> {

    public void sendMsg(String userId, String title, String content, String projectId) {
        DigitalizeMessage componentMessage = new DigitalizeMessage();
        componentMessage.setTitle(title);
        componentMessage.setContent(content);
        componentMessage.setMessageStatus(0);
        componentMessage.setDelStatus(0);
        componentMessage.setUserId(userId);
        componentMessage.setProjectId(projectId);
        this.save(componentMessage);
    }
    /**
     * 我的消息
     *
     * @return ResultVo<MyMessageVo>
     */
    public ResultVo<Page<DigitalizeMessage>> pageList(MessageQuery query) {
        Page<DigitalizeMessage> page =  new Page<DigitalizeMessage>(query.getPageNo(), query.getPageSize());
        page = this.lambdaQuery()
                .eq(DigitalizeMessage::getProjectId, query.getProjectId())
                .eq(DigitalizeMessage::getUserId, UserUtils.getUser().getId())
                .eq(DigitalizeMessage::getMessageStatus, query.getMessageStatus())
                .orderByDesc(DigitalizeMessage::getCreateDate)
                .page(page);
        return ResultVo.success(page);
    }

    /**
     * 清空消息
     *
     * @return ResultVo<Object>
     */
    public ResultVo<Object> empty() {
        User user = UserUtils.getUser();
        String currentUserId = user.getId();
        this.lambdaUpdate()
                .eq(DigitalizeMessage::getUserId, currentUserId)
                .set(DigitalizeMessage::getDelStatus, Constants.DEL_STATUA_1)
                .update(new DigitalizeMessage());
        return ResultVo.success();
    }

    /**
     * 标记已读
     * @return ResultVo<Object>
     */
    public ResultVo<Object> read(List<Long> idList) {
        this.lambdaUpdate()
                .in(DigitalizeMessage::getId, idList)
                .set(DigitalizeMessage::getMessageStatus, Constants.MESSAGE_STATUS_1)
                .update(new DigitalizeMessage());
        return ResultVo.success();
    }
}